package com.warm.androidstudy.utils;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import java.lang.reflect.Field;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Administrator on 2017/6/28 0028.
 */

public class KeyboardUtils {
    public KeyboardUtils() {
    }

    public static void hideSoftKeyboard(Context context, View view) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null && null != view) {
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

    public static void HideKeyboard(View v) {
        InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
        }
    }

    public static void ShowKeyboard(View v) {
        InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(v, 2);
    }

    public static void KeyBoard(final EditText txtSearchKey, final String status) {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
                InputMethodManager m = (InputMethodManager) txtSearchKey.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                if (status.equals("open")) {
                    m.showSoftInput(txtSearchKey, 2);
                } else {
                    m.hideSoftInputFromWindow(txtSearchKey.getWindowToken(), 0);
                }

            }
        }, 300L);
    }

    public static void TimerHideKeyboard(final View v) {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
                InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm.isActive()) {
                    imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
                }

            }
        }, 10L);
    }

    public static boolean KeyBoard(EditText edittext) {
        boolean bool = false;
        InputMethodManager imm = (InputMethodManager) edittext.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm.isActive()) {
            bool = true;
        }

        return bool;
    }


    public static void hideSoftInput(final Activity activity) {
        View view = activity.getCurrentFocus();
        if (view == null) {
            view = new View(activity);
        }
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        if (imm == null) {
            return;
        }
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    public static void fixInputMethodManagerLeak(Context destContext) {
        if (destContext != null) {
            InputMethodManager imm = (InputMethodManager) destContext.getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null) {
                String[] arr = new String[]{"mCurRootView", "mServedView", "mNextServedView"};
                Field f = null;
                Object obj_get = null;
                String[] var5 = arr;
                int var6 = arr.length;

                for (int var7 = 0; var7 < var6; ++var7) {
                    String param = var5[var7];

                    try {
                        f = imm.getClass().getDeclaredField(param);
                        if (!f.isAccessible()) {
                            f.setAccessible(true);
                        }

                        obj_get = f.get(imm);
                        if (obj_get != null && obj_get instanceof View) {
                            View t = (View) obj_get;
                            if (t.getContext() != destContext) {
                                LogUtil.e("fixInputMethodManagerLeak break, context is not suitable, get_context=" + t.getContext() + " dest_context=" + destContext);
                                break;
                            }

                            f.set(imm, (Object) null);
                        }
                    } catch (Throwable var10) {
                        var10.printStackTrace();
                    }
                }

            }
        }
    }
}
